package com.shushan.pages.newtype.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shushan.pages.newtype.bean.EnglishWordsEntryItemBean;
import com.shushan.shushanbao.R;

import java.util.List;

/**
 * @ClassName: EnglishWordsRecyclerAdapter
 * @Desciption: //英语单词recyclerView适配器
 * @author: zhangshihao
 * @date: 2018-06-27
 */
public class EnglishWordsRecyclerAdapter extends RecyclerView.Adapter<EnglishWordsRecyclerAdapter.ViewHolder>{

    private List<EnglishWordsEntryItemBean> entryItemBeans;
    private OnItemClickListener onItemClickListener;

    public EnglishWordsRecyclerAdapter(List<EnglishWordsEntryItemBean> entryItemBeans){
        this.entryItemBeans = entryItemBeans;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_english_words_recycler,null,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        EnglishWordsEntryItemBean itemBean = entryItemBeans.get(position);

        holder.tvWords.setText(itemBean.new_word);
        holder.tvSoundMark.setText(itemBean.pronunciation);
        holder.tvTranslation.setText(itemBean.chinese_name);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(onItemClickListener != null){
                    onItemClickListener.onItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return entryItemBeans.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView tvWords;
        private TextView tvSoundMark;
        private TextView tvTranslation;
        public ViewHolder(View itemView) {
            super(itemView);
            tvWords = itemView.findViewById(R.id.tv_words);
            tvSoundMark = itemView.findViewById(R.id.tv_soundmark);
            tvTranslation = itemView.findViewById(R.id.tv_translation);
        }
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public interface OnItemClickListener{
        void onItemClick(int position);
    }

}
